package marco.android.CouchControl;

import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerManager
{
	/** Accuracy configuration */
//	private static float mThreshold = 0.2f;
//	private static int mInterval    = 1000;
	private static SensorManager mSensorManager;

	/**
	 * Unregisters listeners
	 */
	public static void stopListening(SensorEventListener sensorEventListener)
	{
		if(mSensorManager != null && sensorEventListener != null)
		{
			mSensorManager.unregisterListener(sensorEventListener);
		}
	}

	/**
	 * Returns true if at least one Accelerometer sensor is available
	 */
	public static boolean isSupported(Context c)
	{
		mSensorManager = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
		List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		return(sensors.size() > 0);
	}

//	/**
//	 * Configure the listener for shaking
//	 * @param threshold
//	 *             minimum acceleration variation for considering shaking
//	 * @param interval
//	 *             minimum interval between to shake events
//	 */
//	public static void configure(int threshold, int interval)
//	{
//		AccelerometerManager.mThreshold = threshold;
//		AccelerometerManager.mInterval = interval;
//	}

	/**
	 * Registers a listener and start listening
	 * @param accelerometerListener
	 *        callback for accelerometer events
	 */
	public static boolean startListening(Context c, SensorEventListener sensorEventListener, Sensor sens)
	{
		mSensorManager = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
		List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		for(Sensor s : sensors)
		{
			sens = s;
			if(mSensorManager.registerListener(sensorEventListener, s, SensorManager.SENSOR_DELAY_GAME) == true)
			{
				return true;
			}
		}
		return(false);
	}

	/**
	 * Configures threshold and interval
	 * And registers a listener and start listening
	 * @param accelerometerListener
	 *             callback for accelerometer events
	 * @param threshold
	 *             minimum acceleration variation for considering shaking
	 * @param interval
	 *             minimum interval between to shake events
	 */
//	public static void startListening(Context c, SensorEventListener sensorEventListener, int threshold, int interval)
//	{
////		configure(threshold, interval);
//		startListening(c, sensorEventListener);
//	}

	/**
	 * The listener that listen to events from the accelerometer listener
	 */
//	private static SensorEventListener sensorEventListener = 
//		new SensorEventListener() {
//
//		private long now = 0;
//		private long timeDiff = 0;
//		private long lastUpdate = 0;
//		private long lastShake = 0;
//
//		private float x = 0;
//		private float y = 0;
//		private float z = 0;
//		private float lastX = 0;
//		private float lastY = 0;
//		private float lastZ = 0;
//		private float force = 0;
//
//		public void onAccuracyChanged(Sensor sensor, int accuracy) {}
//
//		public void onSensorChanged(SensorEvent event) {
//			// use the event timestamp as reference
//			// so the manager precision won't depends 
//			// on the AccelerometerListener implementation
//			// processing time
//			now = event.timestamp;
//
//			x = event.values[0];
//			y = event.values[1];
//			z = event.values[2];
//
//			// if not interested in shake events
//			// just remove the whole if then else bloc
//			if (lastUpdate == 0) {
//				lastUpdate = now;
//				lastShake = now;
//				lastX = x;
//				lastY = y;
//				lastZ = z;
//			} else {
//				timeDiff = now - lastUpdate;
//				if (timeDiff > 0) {
//					force = Math.abs(x + y + z - lastX - lastY - lastZ) 
//					/ timeDiff;
//					if (force > threshold) {
//						if (now - lastShake >= interval) {
//							// trigger shake event
//							listener.onShake(force);
//						}
//						lastShake = now;
//					}
//					lastX = x;
//					lastY = y;
//					lastZ = z;
//					lastUpdate = now;
//				}
//			}
//			// trigger change event
//			listener.onAccelerationChanged(x, y, z);
//		}
//
//	};
}
